package practise.FairandSquare;

import java.io.PrintStream;
import java.math.BigInteger;
import java.util.Scanner;

public class Bate1
{
	private static Scanner in = new Scanner(System.in);
	private static PrintStream out = System.out;
	
	public static void main(String[] args)
	{
		int current = in.nextInt()+1;
		for(int i=1; i<current; i++)
		{
			BigInteger ba = new BigInteger(in.next());
			BigInteger bb = new BigInteger(in.next());
			
			BigInteger bs = bigSqrt(BigInteger.ONE, ba);
			BigInteger be = bigSqrt(ba, bb);
			if(be.pow(2).compareTo(bb) == 0)
				be = be.add(BigInteger.ONE);
			
			int count = 0;
			while(bs.compareTo(be) < 0)
			{
				if(isPalindromes(bs) && isPalindromes(bs.pow(2)))
					count++;

				bs = bs.add(BigInteger.ONE);
			}
			
			System.out.println("Case #" + i + ": " + count);
		}
	}

	private static boolean isPalindromes(BigInteger bs)
	{
		char[] ch = bs.toString().toCharArray();
		
		for(int i=0, j=ch.length-1; i<j; i++,j--)
		{
			if(ch[i] != ch[j])
				return false;
		}
		
		return true;
	}

	private static BigInteger bigSqrt(BigInteger s, BigInteger c)
	{
		while(c.compareTo(s.pow(2)) > 0)
		{
			s = s.add(BigInteger.ONE);
		}

		return s;
	}
}
